package com.czk.ssafunctioncalling.controller;

import com.czk.ssafunctioncalling.util.DateTimeTools;
import jakarta.annotation.Resource;
import org.springframework.ai.chat.client.ChatClient;
import org.springframework.ai.chat.model.ChatModel;
import org.springframework.ai.chat.prompt.Prompt;
import org.springframework.ai.model.tool.ToolCallingChatOptions;
import org.springframework.ai.support.ToolCallbacks;
import org.springframework.ai.tool.ToolCallback;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import reactor.core.publisher.Flux;

/**
 * @Author: chenzhangkun
 * @CreateTime: 2025-11-02
 * @Description:
 * @Version: 1.0
 */
@RestController
public class ToolController {
    @Resource(name = "deepseek")
    private ChatModel chatModel;
    @Resource(name = "qwenChatClient")
    private ChatClient chatClient;

    @GetMapping(value = "/tool/call")
    public String chat(@RequestParam(name = "msg", defaultValue = "你是谁，现在几点了") String msg) {
        // 工具注册到工具集合里面
        ToolCallback[] tools = ToolCallbacks.from(new DateTimeTools());
        // 将工具集配置进ChatOptions对象
        ToolCallingChatOptions toolCallingChatOptions = ToolCallingChatOptions.builder().toolCallbacks(tools).build();
        // 提示词
        Prompt prompt = new Prompt(msg, toolCallingChatOptions);
        // 调用
        return chatModel.call(prompt).getResult().getOutput().getText();


    }

    @GetMapping(value = "/tool/call2")
    public Flux<String> chatClient(@RequestParam(name = "msg", defaultValue = "你是谁，现在几点了") String msg) {
        return chatClient
                .prompt(msg)
                .tools(new DateTimeTools())
                .stream()
                .content();


    }
}
